import {
  logger
} from '../../helpers/logger'
export default {
  state: {
    authenticated: false,
    email: '',
    name: ''
  },
  mutations: {
    setUser(state, user) {
      state.authenticated = true,
        state.email = user.email,
        state.name = user.name
    },
    unsetUser(state) {
      state.authenticated = false,
        state.email = null,
        state.name = null
    },
    setUserName(state, value) {
      state.name = value
    },
    setUserEmail(state, value) {
      state.email = value
    },
  },
  actions: {
    setAuthUser({
      commit,
      dispatch
    }) {
      return axios.get('/api/user').then(res => {
        commit('setUser', res.data)
      }).catch(err => {
        if (err.response.status && err.response.status === 401) {
          logger('refresh token request')
          return dispatch('refreshToken')
        }
        logger(err.response)
      })
    },
  },
  getters: {
    getAuthUser(state) {
      return state
    }
  }
}
